package com.ml.controller;

import com.ml.dao.KqAccessBaseDao;
import com.ml.model.AjaxMsg;
import com.ml.model.MLlog;
import com.ml.service.MLUserService;
import com.ml.service.MLlogService;
import com.ml.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

/**用来系统检测*/
@Controller
@RequestMapping(value = "sys/test/")
public class SystemTestController {
    @Autowired
    private SystemService systemService;

    @Autowired
    private MLUserService mLUserService;



    @Autowired
    private MLlogService mLlogService;


    /**跳转到系统检测首页*/
    @RequestMapping({"index"})
    public String list() {
        return "systemtest.jsp";
    }



    /**
     * 检测 上海的 access 的 rmi 中间链接 是否打通
     */
    @RequestMapping({"kq/connect/sh"})
    @ResponseBody
    public AjaxMsg testConnectSh(HttpServletRequest request, HttpServletResponse response) {
        KqAccessBaseDao kqAccessBaseDao = new KqAccessBaseDao();
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        Connection con = null;
        String msg = "fail";
        try {
            con = kqAccessBaseDao.openConnectSh();
        }catch (Exception e){
            mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到上海考勤 rmijdbc 服务异常。",request));//保存个日志
        }finally {
            if(con!=null){
                msg = "success";
                kqAccessBaseDao.closeConnect(con);
            }else{
                mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到上海考勤 rmijdbc 服务异常。",request));//保存个日志
            }
        }
        return AjaxMsg.returnAjaxMsg(200,msg);
    }
    /**
     * 检测 天津 的 access 的 rmi 中间链接 是否打通
     */
    @RequestMapping({"kq/connect/tj"})
    @ResponseBody
    public AjaxMsg testConnectTj(HttpServletRequest request, HttpServletResponse response) {
        KqAccessBaseDao kqAccessBaseDao = new KqAccessBaseDao();
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        Connection con = null;
        String msg = "fail";
        try {
            con = kqAccessBaseDao.openConnectTj();
        }catch (Exception e){
            mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到天津考勤 rmijdbc 服务异常。",request));//保存个日志
        }finally {
            if(con!=null){
                msg = "success";
                kqAccessBaseDao.closeConnect(con);
            }else{
                mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到天津考勤 rmijdbc 服务异常。",request));//保存个日志
            }
        }
        return AjaxMsg.returnAjaxMsg(200,msg);
    }
    /**
     * 检测 大连 的 access 的 rmi 中间链接 是否打通
     */
    @RequestMapping({"kq/connect/dl"})
    @ResponseBody
    public AjaxMsg testConnectDl(HttpServletRequest request, HttpServletResponse response) {
        KqAccessBaseDao kqAccessBaseDao = new KqAccessBaseDao();
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        Connection con = null;
        String msg = "fail";
        try {
            con = kqAccessBaseDao.openConnectDl();
        }catch (Exception e){
            mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到大连考勤 rmijdbc 服务异常。",request));//保存个日志
        }finally {
            if(con!=null){
                msg = "success";
                kqAccessBaseDao.closeConnect(con);
            }else{
                mLlogService.save(new MLlog(1,"考勤检测",dateStr + "检查到大连考勤 rmijdbc 服务异常。",request));//保存个日志
            }
        }
        return AjaxMsg.returnAjaxMsg(200,msg);
    }


    @RequestMapping({"kx"})
    public String kx() {
        return "page/wk/wk_kx.jsp";
    }




}
